import subprocess as sp
import time

from GlobalValues import globalVal


def live():
    # 防止多线程时 command 未被设置
    while True:
        if len(globalVal.command) > 0:
            # 管道配置，其中用到管道
            p = sp.Popen(globalVal.command, stdin=sp.PIPE)
            break
    while True:
        if not globalVal.bl_live:
            print('结束')
            return
        if globalVal.camera_conf:
            p = sp.Popen(globalVal.command, stdin=sp.PIPE)
            globalVal.camera_conf = False
        if not globalVal.frame_queue.empty():
            # 从队列中取出图片
            frame = globalVal.frame_queue.get()
            try:
                p.stdin.write(frame.tostring())
            except Exception as e:
                print(e)
                p = sp.Popen(globalVal.command, stdin=sp.PIPE)
                time.sleep(2)
